<?php
//栏目管理
namespace App\Http\Controllers\App\Content;

use App\Models\ArticleCategory;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ArticleCategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $res = ArticleCategory::where('parent_id', $request->get('parent_id', 0))->orderBy('id', 'desc')->orderBy('sort', 'desc')->paginate($request->get('limit', 30))->toArray();
            return $this->success('Success', $res['data'], $res['total']);
        }
        $categories = (new ArticleCategory())->treeData();
        return view('app.content.category', compact('categories'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        $categories = (new ArticleCategory())->treeData();
        return view('app.content.categoryform', compact('categories'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name'      => 'required|string',
            'sort'      => 'required|numeric',
            'parent_id' => 'required|numeric'
        ]);
        if (ArticleCategory::create($request->all())) {
            return redirect(route('admin.category'))->with(['status' => '添加完成']);
        }
        return redirect(route('admin.category'))->with(['status' => '系统错误']);
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id)
    {
        $category   = ArticleCategory::findOrFail($id);
        $categories = (new ArticleCategory())->treeData();
        return view('app.content.categoryform', compact('category', 'categories'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'name'      => 'required|string',
            'sort'      => 'required|numeric',
            'parent_id' => 'required|numeric'
        ]);
        $category = ArticleCategory::findOrFail($id);
        if ($category->update($request->all())) {
            return redirect(route('admin.category'))->with(['status' => '更新成功']);
        }
        return redirect(route('admin.category'))->withErrors(['status' => '系统错误']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)) {
            return response()->json(['code' => 1, 'msg' => '请选择删除项']);
        }
        $category = ArticleCategory::with(['childs', 'articles'])->find($ids);
        if (!$category) {
            return response()->json(['code' => 1, 'msg' => '请选择删除项']);
        }
        if (!$category->childs->isEmpty() || !$category->articles->isEmpty()) {
            return response()->json(['code' => 1, 'msg' => '该分类下有子分类或者文章，不能删除']);
        }
        if ($category->delete()) {
            return response()->json(['code' => 0, 'msg' => '删除成功']);
        }
        return response()->json(['code' => 1, 'msg' => '删除失败']);
    }
}
